package it.infodea.tapestrydea.pages.demodea;

import it.infodea.tapestrydea.support.annotations.PageInfo;
import it.infodea.tapestrydea.support.enumerations.SecurityGroup;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.Path;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.TextStreamResponse;

@PageInfo(allowedSecurityGroup=SecurityGroup.ANONYMOUS, mode = PageInfo.MODE_NORMAL)
public class JsWindows {

	private static List<Item> items;

	private int indice;
	private Item item;

	@Inject @Path("forjswin/README.html")
	private Asset readMe;

	@Inject @Path("forjswin/register.html")
	private Asset register;
	
	@SetupRender
	void setup(){
		if(item == null){
			items = new ArrayList<Item>();
			items.add(new Item("Read me", readMe));
			items.add(new Item("Register", register));
		}
	}

	StreamResponse onAction(int indice){
		return new TextStreamResponse("text/html", items.get(indice).getHtml());
	}
	
	public int getIndice() {
		return indice;
	}


	public void setIndice(int indice) {
		this.indice = indice;
	}


	public Item getItem() {
		return item;
	}


	public void setItem(Item item) {
		this.item = item;
	}

	public List<Item> getItems() {
		return items;
	}

	public static class Item {
		String title;
		Asset asset;
		public Item(String title, Asset asset) {
			super();
			this.title = title;
			this.asset = asset;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public String getHtml() {
			try {
				InputStream openStream = asset.getResource().openStream();
				byte[] bytes = new byte[openStream.available()];
				openStream.read(bytes);
				return new String(bytes);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	}


}
